/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import pojo.Taikhoan;


/**
 *
 * @author WMThiet
 */
public class MailUtil {
    private String from;
    private String to;
    private String subject;
    private String text;
    private String pass;
    
    public MailUtil(String to, String subject, String text){
        this.to = to;
        this.subject = subject;
        this.text = text;
    }

    public boolean send(){

        Properties props = new Properties();
        
        try {
        props.load(getClass().getClassLoader().getResourceAsStream("config.properties"));
        pass = props.getProperty("mail.pass");
        from = props.getProperty("mail.defaulfrom");

        Authenticator authen = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from,pass);
            }                
        };

        Session mailSession = Session.getInstance(props, authen);

        MimeMessage message = new MimeMessage(mailSession);
        
        InternetAddress fromAddress = new InternetAddress(from);
        InternetAddress toAddress = new InternetAddress(to);
        
        message.setFrom(fromAddress);
        message.setRecipient(RecipientType.TO, toAddress);
        message.setSubject(subject,"UTF-8");
        
        message.setText(text,"UTF-8");
        message.setHeader("Content-Type", "text/html; charset=UTF-8");

        Transport.send(message);	        
                		
        } 
        catch (Exception e) {
           MyLog.WriteLog(this, e.getMessage());    
           return false;
        }		

        return true;
    }   
    
}
